package com.example.administrator.dc.fragment;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.dc.Activity.HomeActivity;
import com.example.administrator.dc.Activity.RegActivity;
import com.example.administrator.dc.Bean.loginresult;
import com.example.administrator.dc.Bean.loginresult1;
import com.example.administrator.dc.R;
import com.example.administrator.dc.Service.loginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.content.Context.MODE_PRIVATE;

/**
 * A simple {@link Fragment} subclass.
 */
public class TwoFragment extends BaseFragment {
    private EditText phone;
    private EditText password;
    private Button phonelogin;
    private Button reg;


    public void onViewCreated(View view,@Nullable Bundle savedInstanceState) {
        phone= (EditText) view.findViewById(R.id.phone);
        reg= (Button) view.findViewById(R.id.Reg);
        password= (EditText) view.findViewById(R.id.phonepassword);
        phonelogin= (Button) view.findViewById(R.id.phonelogin);

        Retrofit retrofit=new Retrofit.Builder().baseUrl("http://118.89.227.169/home/Api/").
                addConverterFactory(GsonConverterFactory.create()).build();
        final loginService retrofitSerivce= retrofit.create(loginService.class);

        phonelogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone1=phone.getText().toString();
                String password1=password.getText().toString();
                Call<loginresult> call=retrofitSerivce.loginresult1(phone1,password1);
                call.enqueue(new Callback<loginresult>() {
                    @Override
                    public void onResponse(Call<loginresult> call, Response<loginresult> response) {
                        if(response.body().getStatus()==1){
                            Toast.makeText(getActivity(), "登录成功",
                                    Toast.LENGTH_SHORT).show();
                            userid=response.body().getData().getUid();
                            String userid1= String.valueOf(userid);

                            SaveUser(userid1);
                            if(!"".equals(userid)){
                                isCheck=true;
                            }
                            else {
                                isCheck=false;
                            }
                            Intent intent=new Intent();
                            intent.setClass(getActivity(),HomeActivity.class);
                            startActivity(intent);

                        }

                    }

                    @Override
                    public void onFailure(Call<loginresult> call, Throwable t) {

                    }
                });

            }
        });

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(v.getContext(), RegActivity.class);
                v.getContext().startActivity(intent);
            }
        });

    }
    public void SaveUser(String userid){
        final SharedPreferences sharedPreferences=getActivity().getSharedPreferences("UserInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();//获取编辑器
        //int uid=Integer.parseInt(userid);
        String uid=userid;
        editor.putString("user_id", uid);
        //存开关状态;
        editor.commit();
        isCheck=true;
    }
    private void getUser(){
        SharedPreferences sp = getActivity().getSharedPreferences("UserInfo", getActivity().MODE_PRIVATE);
        userid = Integer.valueOf(sp.getString("user_id", null));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_two, container, false);
    }

}
